package cn.tedu.bece.admin.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.tedu.bece.admin.common.exception.ServiceException;
import cn.tedu.bece.admin.system.mapper.SysDictMapper;
import cn.tedu.bece.admin.system.mapper.SysDictOptionMapper;
import cn.tedu.bece.admin.system.pojo.dto.SysDictOptionAddParam;
import cn.tedu.bece.admin.system.pojo.dto.SysDictOptionQuery;
import cn.tedu.bece.admin.system.pojo.dto.SysDictOptionUpdateParam;
import cn.tedu.bece.admin.system.pojo.entity.SysDictOption;
import cn.tedu.bece.admin.system.service.ISysDictOptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class SysDictOptionServiceImpl implements ISysDictOptionService {

    @Autowired
    private SysDictOptionMapper sysDictOptionMapper;

    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public void insert(SysDictOptionAddParam sysDictOptionAddParam) {
        if (sysDictMapper.selectById(sysDictOptionAddParam.getDictId()) == null) {
            throw new ServiceException("字典不存在");
        }
        SysDictOption sysDictOption = new SysDictOption();
        BeanUtils.copyProperties(sysDictOptionAddParam, sysDictOption);
        sysDictOptionMapper.insert(sysDictOption);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        sysDictOptionMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SysDictOptionUpdateParam sysDictOptionUpdateParam) {
        SysDictOption sysDictOption = new SysDictOption();
        BeanUtils.copyProperties(sysDictOptionUpdateParam, sysDictOption);
        sysDictOptionMapper.updateById(sysDictOption);
    }

    @Override
    public SysDictOption selectById(Long id) {
        return sysDictOptionMapper.selectById(id);
    }

    @Override
    public List<SysDictOption> selectList(SysDictOptionQuery query) {
        return sysDictOptionMapper.selectSysDictOptionList(query);
    }

}
